package com.res.share.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.res.share.model.ResHistory;
import com.res.share.mapper.ResHistoryMapper;
import com.res.share.service.IResHistoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.res.share.service.IResStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 足迹 服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2021-09-20
 */
@Service
public class ResHistoryServiceImpl extends ServiceImpl<ResHistoryMapper, ResHistory> implements IResHistoryService {

    @Autowired
    private IResStatisticsService resStatisticsService;

    @Override
    public Integer saveHistory(Integer articleId, Integer userId) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("ART_ID",articleId);
        queryWrapper.eq("CREATE_USER_ID",userId);
        queryWrapper.eq("IS_DEL",0);
        ResHistory resHistory = baseMapper.selectOne(queryWrapper);
        if (resHistory == null) {
            resHistory=new ResHistory();
            resHistory.setArtId(articleId);
            resHistory.setCreateUserId(userId);
            baseMapper.insert(resHistory);

            resStatisticsService.updateResHistoryNums(userId,2,1);

        }
        return 1;
    }

    @Override
    public IPage<ResHistory> findHistoryList(Page<ResHistory> page, Integer userId) {
        return baseMapper.findHistoryList(page,userId);
    }

    @Override
    public void updateBatchDelHistory(List<String> ids, Integer userId) {
        baseMapper.updateBatchDelHistory(ids,userId);

        resStatisticsService.updateResHistoryNums(userId,1,ids.size());

    }
}
